package com.demo2do.fms.admin.web.controller.supplier.appraisal;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.AppraisalTemplateItemService;
import com.demo2do.fms.admin.service.supplier.AppraisalTemplateService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.appraisal.AppraisalTemplate;
import com.demo2do.fms.entity.appraisal.AppraisalTemplateItem;

/**
 * 绩效考核标准管理
 * 
 * Created by zhonghui on 2017/7/3.
 * @author Ark
 * 
 */
@Controller
@RequestMapping("/appraisal-templates")
@MenuSetting("menu-supplier")
public class AppraisalTemplateController {
    
    private static final Logger logger = LoggerFactory.getLogger(AppraisalTemplateController.class);
    
    @Autowired
    private AppraisalTemplateService appraisalTemplateSerive;
    
    @Autowired
    private AppraisalTemplateItemService appraisalTemplateItemService;

    /**
     * list
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-performance-appraisal-standard-management")
    public ModelAndView list(@Secure Login login, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-template/appraisal-template-list");
        
        int totals = appraisalTemplateSerive.count(login);
        if (totals > 0) {

            List<AppraisalTemplate> templates = appraisalTemplateSerive.list(login, page.with(totals));

            modelAndView.addObject("templates", templates);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }

    /**
     * view
     *
     * @param id
     * @param page
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-performance-appraisal-standard-management")
    public ModelAndView view(@PathVariable("id") Long id, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-template/appraisal-template-view");
        
        AppraisalTemplate template = appraisalTemplateSerive.load(AppraisalTemplate.class, id);
        modelAndView.addObject("template", template);
        
        int totals = appraisalTemplateItemService.count(template);
        if (totals > 0) {

            List<AppraisalTemplateItem> templateItems = appraisalTemplateItemService.list(template, page.with(totals));
            modelAndView.addObject("templateItems", templateItems);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("totals", totals);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * create panel
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create() {
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-template/appraisal-template-create");
        return modelAndView;
    }

    /**
     * create
     *
     * @param login
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(AppraisalTemplate template, @Secure Login login) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            appraisalTemplateSerive.create(template, login);
            
            httpResult.success().message("添加成功");
            
        } catch (Exception e) {
            logger.error("#onCreate() - create occur exception [{}]", e);
            httpResult.fail("500").message("添加失败! " + e.getMessage());
        }
        
        return httpResult;
        
    }
    
    /**
     * edit panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-template/appraisal-template-edit");
        
        AppraisalTemplate template = appraisalTemplateSerive.load(AppraisalTemplate.class, id);
        
        modelAndView.addObject("template", template);
        
        return modelAndView;
        
    }
    
    /**
     * edit
     * 
     * @param changeTemplate
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@PathVariable("id") Long id,
                                           AppraisalTemplate changeTemplate, 
                                           @Secure(property = "id") Long operator) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            AppraisalTemplate oldTemplate = appraisalTemplateSerive.load(AppraisalTemplate.class, id);
            appraisalTemplateSerive.edit(oldTemplate, changeTemplate, operator);
            
            httpResult.success().message("编辑成功");
            
        } catch (Exception e) {
            logger.error("#onEdit() - edit occur exception [{}]", e);
            httpResult.fail("500").message("编辑失败! " + e.getMessage());
        }
        
        return httpResult;
        
    }
    
    /**
     * delete panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-template/appraisal-template-delete");
        
        modelAndView.addObject("id", id);
        
        return modelAndView;
        
    }
    
    /**
     * delete
     * 
     * @param id
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onDelete(@PathVariable("id") Long id, @Secure(property = "id") Long operator) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            AppraisalTemplate template = appraisalTemplateSerive.load(AppraisalTemplate.class, id);
            appraisalTemplateSerive.delete(template, operator);
            
            httpResult.success().message("删除成功");
            
        } catch (Exception e) {
            logger.error("#onDelete() - delete occur exception [{}]", e);
            httpResult.fail("500").message("删除失败! " + e.getMessage());
        }
        
        return httpResult;
        
    }
    
    /**
     * create item panel
     * 
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/{templateId}/items/create", method = RequestMethod.GET)
    public ModelAndView createItem(@PathVariable("templateId") Long templateId) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-template/appraisal-template-item-create");
        
        modelAndView.addObject("templateId", templateId);
        
        return modelAndView;
        
    }
    
    /**
     * create item
     * 
     * @param login
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/{templateId}/items", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateItem(@Secure Login login,
                                                 @PathVariable("templateId") Long templateId,
                                                 AppraisalTemplateItem item) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            AppraisalTemplate template = appraisalTemplateSerive.load(AppraisalTemplate.class, templateId);
            appraisalTemplateItemService.create(item, template, login);
            
            httpResult.success().message("添加成功");
            
        } catch (Exception e) {
            logger.error("#onCreateItem() - create occur exception [{}]", e);
            httpResult.fail("500").message("添加失败! " + e.getMessage());
        }
        
        return httpResult;
        
    }
    
    /**
     * edit item panel
     * 
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/{templateId}/items/{itemId}/edit", method = RequestMethod.GET)
    public ModelAndView editItem(@PathVariable("templateId") Long templateId,
                                 @PathVariable("itemId") Long itemId) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-template/appraisal-template-item-edit");
        
        AppraisalTemplateItem item = appraisalTemplateItemService.load(AppraisalTemplateItem.class, itemId);
        
        modelAndView.addObject("templateId", templateId);
        modelAndView.addObject("item", item);
        
        return modelAndView;
        
    }
    
    /**
     * edit item
     * 
     * @param login
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/{templateId}/items/{itemId}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditItem(@Secure Login login,
                                               @PathVariable("templateId") Long templateId,
                                               @PathVariable("itemId") Long itemId,
                                               AppraisalTemplateItem changeItem) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            AppraisalTemplate template = appraisalTemplateSerive.load(AppraisalTemplate.class, templateId);
            AppraisalTemplateItem oldItem = appraisalTemplateItemService.load(AppraisalTemplateItem.class, itemId);
            
            appraisalTemplateItemService.edit(oldItem, changeItem, template, login);
            
            httpResult.success().message("编辑成功");
            
        } catch (Exception e) {
            logger.error("#onEditItem() - edit occur exception [{}]", e);
            httpResult.fail("500").message("编辑失败! " + e.getMessage());
        }
        
        return httpResult;
        
    }
    
    /**
     * delete item panel
     * 
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/{templateId}/items/{itemId}/delete", method = RequestMethod.GET)
    public ModelAndView deleteItem(@PathVariable("templateId") Long templateId,
                                 @PathVariable("itemId") Long itemId) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-template/appraisal-template-item-delete");
        
        modelAndView.addObject("templateId", templateId);
        modelAndView.addObject("itemId", itemId);
        
        return modelAndView;
        
    }
    
    /**
     * delete item
     * 
     * @param login
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/{templateId}/items/{itemId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onDeleteItem(@Secure Login login,
                                                 @PathVariable("templateId") Long templateId,
                                                 @PathVariable("itemId") Long itemId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            AppraisalTemplateItem item = appraisalTemplateItemService.load(AppraisalTemplateItem.class, itemId);
            appraisalTemplateItemService.delete(item, login.getId());
            
            httpResult.success().message("删除成功");
            
        } catch (Exception e) {
            logger.error("#onDeleteItem() - delete occur exception [{}]", e);
            httpResult.fail("500").message("删除失败! " + e.getMessage());
        }
        
        return httpResult;
        
    }
    
}
